function selectType(){
    var type=document.getElementById("db_engine").value;
    var el = document.getElementById("mongo_auth");
    var port=document.getElementById("db_port");
	var auth = document.getElementById('auth');
    if (type=='mongo')
    {
		auth.style.display = "inline";
        port.value="27017";
		onShow();
    }else {
        el.style.display="none";
		auth.style.display="none";
        port.value="3306";
    }
}


function hiddenDiv(el,ishidden)
{
	var d = ishidden ? "none" : "inline";
	var p = el.parentNode.parentNode.style.display=d;
}


function onShow()
{
	var el = document.getElementById("mongo_auth");
	if(document.install.isauth.checked)
	{
		el.style.display="inline";
		hiddenDiv(document.install.db_user, false);
		hiddenDiv(document.install.db_passwd, false);
	}else{
		el.style.display="none";
		hiddenDiv(document.install.db_user, true);
		hiddenDiv(document.install.db_passwd, true);
	}
}



function writeMessage(el, message)
{
	var p = el.parentNode.parentNode;
	var m = p.children[0].innerHTML.trim();		
	document.getElementById('message').innerHTML=m+message;
	el.focus();
}


function notNull(){
	document.getElementById('message').innerHTML='';
    var passwd = ''
	var els = document.install.elements;
	var postString = ''
    for (var i = 0; i < document.install.length; i++)
	{
		// 不为空检测
		if(els[i].value == "")
        {
			if((els[i].name == 'db_user' && document.install.isauth.checked)		
			   && (els[i].name != 'db_passwd'))
			{
				writeMessage(els[i],"不允许为空");
				return false;
			}
		}
		
		// 密码检测
        if(els[i].name == 'password')
			passwd = els[i].value;
		if(els[i].name == 'retypepass')
		{
			if (passwd != els[i].value){
				writeMessage(els[i], '两次密码不相同');
				els[i].value='';
				var t = i-1;
				els[t].value='';
				return false;
			}
		}

		if(els[i].name == 'email')
		{
			var reg = /^[-0-9a-zA-Z_.]+@{1}([0-9a-zA-Z]+\.){1,2}[0-9a-zA-Z]+$/;
			if(!reg.exec(els[i].value)){
				writeMessage(els[i], '格式不正确');
				return false;
			}
		}

		if(els[i].name == 'isauth')
			postString += els[i].name + '=' + els[i].checked + '&';
		else
			postString += els[i].name + '=' + els[i].value + '&';
	}
	return postString;
}



/* Ajax */
var xmlHttp;

function getXmlHttp(){
    if(window.ActiveXObject){
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }else if(window.XMLHttpRequest){
        xmlHttp = new XMLHttpRequest();
    }
}


function Send(){ 
    getXmlHttp();
    if(xmlHttp != null)
    {
		var post_str=notNull()
		if (post_str){
			xmlHttp.onreadystatechange = ShowMessage;
			xmlHttp.open("POST", '/install/post/', true);
			xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			xmlHttp.send(post_str);
		}
    }
}
function ShowMessage()
{
    if(xmlHttp.readyState == 4 && xmlHttp.status == 200) 
	{	
        if(xmlHttp.responseText.length > 0)
		{
			result = eval("("+xmlHttp.responseText+")");
			alert(result.message);
			if(result.status)
			{
				location.href='/';
			}
		}       
        else    
		{     
			alert('安装失败');
		}       

    }
}